package com.google.code.UtopiaXml;

import java.util.ArrayList;
import java.util.List;

/**
 * Abstract class representing an XSD Indicator (Sequence, Choice and All). It
 * contains Elements or another Indicators. Relationships should never be cyclic
 * - you shouldn't add an Indicator into itself.
 *
 * @author Michael Simacek
 */
public abstract class Indicator implements IndicatorContent {

    private List<IndicatorContent> content;

    /**
     * Constructs empty Indicator with given name
     *
     */
    public Indicator() {
        content = new ArrayList<>();
    }

    /**
     * Makes a deep copy of this indicator
     *
     * @return deep copy
     */
    public Indicator copy() {
        Indicator ret = shallowCopy();
        ret.content = new ArrayList<>(content);
        return ret;
    }

    /**
     * Makes a shallow copy (contents are shared)
     *
     * @return shallow copy of this indicator
     */
    protected abstract Indicator shallowCopy();

    /**
     * Adds content to the Indicator.
     *
     * @param item item to be added
     */
    public void add(IndicatorContent item) {
        content.add(item);
    }

    /**
     * Returns all items in the indicator.
     *
     * @return List of all items
     */
    public List<IndicatorContent> getContent() {
        return content;
    }

    @Override
    public List<String> getMatchStrings() {
        List<String> list = new ArrayList<>();
        for (IndicatorContent item : content) {
            list.addAll(item.getMatchStrings());
        }
        return list;
    }
}
